﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;

namespace MainGame.Menu
{

  public interface IAudioSettings
  {
  }

  public class AudioSettings : BaseMenu, IAudioSettings
  {
    Button[] volumeBars, volumeBtn;
    Button soundFX;
    public bool onClick = false, backClicked = false, menuSwitched = false;
    SpriteBatch spriteBatch;
    public int Vol = 0, timer = 7, pressedBtn, grow = 1, direction = 1, AnimVelocity = 160;
    private int VOLUMEBARS_NUM = 5;
    //private float musicVolume;
    Color colorDisplayString = Color.Red;
    Texture2D soundOnTexture, soundOffTexture;
    SpriteFont baveuse;


    public AudioSettings(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = null;
      bounds = new Rectangle(game.GraphicsDevice.Viewport.Bounds.Center.X, 40, 220, 330);
      //game.Services.AddService(typeof(IAudioSettings), this);
    }

    protected override void Dispose(bool disposing)
    {
      //Game.Services.RemoveService(typeof(IAudioSettings));
      base.Dispose(disposing);
    }

    public override void Initialize()
    {
      spriteBatch = Game.GetContext().SpriteBatch;

      volumeBars = new Button[VOLUMEBARS_NUM];
      volumeBtn = new Button[2];

      int volBarDist = 20;
      int numBarsToColor;

      soundOnTexture = Game.Content.Load<Texture2D>(@"Images//Menu//SoundON");
      soundOffTexture = Game.Content.Load<Texture2D>(@"Images//Menu//SoundOFF");

      baveuse = Game.Content.Load<SpriteFont>("baveuse1_font");

      for (int i = 0; i < VOLUMEBARS_NUM; i++)
      {
        var curr_i = i;

        volumeBars[i] = new Button(Game, @"white_pixel", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 80 + ((i + 1) * 25), (int)bounds.Y + 130 - volBarDist, 15, volBarDist), Color.White, "small_font", "", Color.Yellow);
        AddButton(volumeBars[i], () =>
        {

        });

        volBarDist += 10;
      }

      for (int i = 0; i < 2; i++)
      {
        var curr_i = i;

        volumeBtn[i] = new Button(Game, @"Images//Menu//setVol" + i, new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X + 30 + (i * 210), (int)bounds.Y + 90, 50, 50), Color.PaleTurquoise, "small_font", "", Color.Yellow);
        AddButton(volumeBtn[i], () =>
        {
          // Color the volume bars when volume up/down buttons are pressed
          float volumeStep = 1.0f / VOLUMEBARS_NUM;
          int currentVolume = (int)(Game.GetContext().currentConfig.sfxVolume * VOLUMEBARS_NUM);
          if (curr_i == 0)
          {
            // Volume down pressed
            if (currentVolume > 1)
              Game.GetContext().currentConfig.sfxVolume -= volumeStep;
            else
              Game.GetContext().currentConfig.sfxVolume = 0f;

            //if (Vol > 0)
            //    Vol--;

            //volumeBars[Vol].color = Color.White;
          }
          else
          {
            // Volume up pressed
            if (currentVolume < VOLUMEBARS_NUM - 1)
              Game.GetContext().currentConfig.sfxVolume += volumeStep;
            else
              Game.GetContext().currentConfig.sfxVolume = 1.0f;

            //if (Vol < 4)
            //  Vol++;

            //volumeBars[Vol].color = Color.Tomato;
          }

          numBarsToColor = (int)(Game.GetContext().currentConfig.sfxVolume * VOLUMEBARS_NUM);
          for (int x = 0; x < VOLUMEBARS_NUM; x++)
          { 
            if(x < numBarsToColor)
              volumeBars[x].color = Color.Tomato;
            else
              volumeBars[x].color = Color.White;
          }

          var soundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
          soundManager.SfxVolume = Game.GetContext().currentConfig.sfxVolume;

          pressedBtn = curr_i;
          onClick = true;
        });
      }

      soundFX = new Button(Game, @"Images//menu//SoundOn", new Rectangle(bounds.X + 120, (int)bounds.Y + 220, 60, 60), Color.White, "small_font", "", Color.Yellow);
      AddButton(soundFX, () =>
      {
        if (soundFX.texture.Equals(soundOnTexture))
        {
          soundFX.texture = soundOffTexture;
          Game.GetContext().currentConfig.sfxMuted = true;
        }
        else
        {
          soundFX.texture = soundOnTexture;
          Game.GetContext().currentConfig.sfxMuted = false;
        }

      });

      // Load the settings for the current profile
      numBarsToColor = (int)(Game.GetContext().currentConfig.sfxVolume * VOLUMEBARS_NUM);
      for (int x = 0; x < VOLUMEBARS_NUM; x++)
      {
        if (x < numBarsToColor)
          volumeBars[x].color = Color.Tomato;
        else
          volumeBars[x].color = Color.White;
      }


      if (!Game.GetContext().currentConfig.sfxMuted)
        soundFX.texture = soundOnTexture;
      else
        soundFX.texture = soundOffTexture;

      base.Initialize();
    }
    

    public override void Update(GameTime gameTime)
    {
      //Color the volume buttons when clicked
      if (onClick)
      {
        volumeBtn[pressedBtn].color = Color.SkyBlue;
        timer--;

        if (timer <= 0)
        {
          volumeBtn[pressedBtn].color = Color.Azure;
          onClick = false;
          timer = 10;
        }
      }

      if (backClicked)
      {
        foreach (Button b in (from cb in buttons select cb.b))
        {
          b.bounds.X += AnimVelocity;
          colorDisplayString = Color.Transparent;
        }
      }

      //enlarge the button Fx size
      if (grow < 50)
      {
        grow += 2;
        soundFX.bounds.Width += 2;
        soundFX.bounds.Height += 2;
      }

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();
            
      spriteBatch.DrawString(baveuse, Locale.Strings.SoundVolume, new Vector2(bounds.X + ((bounds.Width - baveuse.MeasureString(Locale.Strings.SoundVolume).X) / 2) + 60, bounds.Y + 10), colorDisplayString);
      spriteBatch.DrawString(baveuse, Locale.Strings.SoundFx, new Vector2(bounds.X + ((bounds.Width - baveuse.MeasureString(Locale.Strings.SoundFx).X) / 2) + 60, bounds.Y + 170), colorDisplayString);

      spriteBatch.End();
    }
  }
}
